package com.campuscarbooking.api.controller;

import com.campuscarbooking.api.dto.request.ReservationRequest;
import com.campuscarbooking.api.dto.response.ReservationResponse;
import com.campuscarbooking.api.model.Reservation;
import com.campuscarbooking.api.service.ReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "预约管理接口")
@RestController
@RequestMapping("/reservations")
@RequiredArgsConstructor
public class ReservationController {

    private final ReservationService reservationService;

    @ApiOperation(value = "获取我的预约列表", notes = "获取当前用户的预约列表，可根据状态筛选")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "status", value = "预约状态 (pending/confirmed/completed/canceled)", required = false, dataTypeClass = String.class),
        @ApiImplicitParam(name = "page", value = "页码", required = false, dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "size", value = "每页大小", required = false, dataTypeClass = Integer.class)
    })
    @GetMapping("/my")
    public ResponseEntity<Page<ReservationResponse>> getMyReservations(
            @RequestParam(required = false) Reservation.Status status,
            Pageable pageable,
            Authentication authentication) {
        Long userId = Long.parseLong(authentication.getName());
        Page<Reservation> reservations = reservationService.getUserReservations(userId, status, pageable);
        Page<ReservationResponse> response = reservations.map(ReservationResponse::fromReservation);
        return ResponseEntity.ok(response);
    }

    @ApiOperation(value = "获取预约详情", notes = "根据ID获取当前用户的预约详情")
    @GetMapping("/{id}")
    public ResponseEntity<ReservationResponse> getReservationById(
            @ApiParam(value = "预约ID", required = true) @PathVariable Long id,
            Authentication authentication) {
        Long userId = Long.parseLong(authentication.getName());
        Reservation reservation = reservationService.getUserReservation(id, userId);
        return ResponseEntity.ok(ReservationResponse.fromReservation(reservation));
    }

    @ApiOperation(value = "创建预约", notes = "为当前用户创建新预约")
    @PostMapping
    public ResponseEntity<ReservationResponse> createReservation(
            @ApiParam(value = "预约信息", required = true) @Valid @RequestBody ReservationRequest request,
            Authentication authentication) {
        Long userId = Long.parseLong(authentication.getName());
        Reservation reservation = reservationService.createReservation(userId, request);
        return ResponseEntity.ok(ReservationResponse.fromReservation(reservation));
    }

    @ApiOperation(value = "取消预约", notes = "取消当前用户的预约")
    @PostMapping("/{id}/cancel")
    public ResponseEntity<ReservationResponse> cancelReservation(
            @ApiParam(value = "预约ID", required = true) @PathVariable Long id,
            Authentication authentication) {
        Long userId = Long.parseLong(authentication.getName());
        Reservation reservation = reservationService.cancelReservation(id, userId);
        return ResponseEntity.ok(ReservationResponse.fromReservation(reservation));
    }

    @ApiOperation(value = "签到预约", notes = "为预约签到（仅管理员和司机）")
    @PostMapping("/{id}/check-in")
    @PreAuthorize("hasAnyRole('ADMIN', 'DRIVER')")
    public ResponseEntity<ReservationResponse> checkInReservation(
            @ApiParam(value = "预约ID", required = true) @PathVariable Long id) {
        Reservation reservation = reservationService.checkInReservation(id);
        return ResponseEntity.ok(ReservationResponse.fromReservation(reservation));
    }
} 